/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StartupCompanyManager2012SideProjcWork.java
 *
 * Created on 10.12.2011, 21:44:46
 */
package startupcompanymanager2012;

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import startupcompanymanager2012.minigames.SlidePuzzle.*;

/**
 *
 * @author Matthäus
 */
public class StartupCompanyManager2012gameWorker extends javax.swing.JFrame {

    private static int totalLines = 0, completedLines = 0, bugs = 0, maxBugs = 0, status = 0, usedtime = 0, selected;
    public static boolean buttonWork, buttonDebug, bonusReady = false;
    private static double cost = 0.00;
    private JFrame bonusBox = new JFrame();

    /** Creates new form StartupCompanyManager2012SideProjcWork */
    public StartupCompanyManager2012gameWorker() {

        initComponents();
        checkBonusAvailable();
        updateStats();
        Core.calendarTimer.stop();



    }

    public static void updateStats() {
        selected = StartupCompanyManager2012gameCreator.gameList.getSelectedIndex();
        if (Core.games.get(selected).isWriting() == true) {
            buttonWork = true;
            buttonDebug = false;
        }
        if (Core.games.get(selected).isDebugging() == true) {
            buttonWork = false;
            buttonDebug = true;
        }
        gameName.setText(Core.games.get(selected).getTitle());
        cost = Core.games.get(selected).getCost();
        gameCost.setText(Core.nfmt.format(cost));
        totalLines = Core.games.get(selected).getTotalLines();
        completedLines = Core.games.get(selected).getCompletedLines();
        gameLineStatus.setText(Integer.toString(completedLines) + "/" + Integer.toString(totalLines));
        maxBugs = Core.games.get(selected).getMaxBugs();
        bugs = Core.games.get(selected).getBugs();
        lineStatusBar.setMaximum(totalLines + maxBugs);
        lineStatusBar.setMinimum(0);
        lineStatusBar.setValue(Core.games.get(selected).getCompletedLines());
        if (completedLines <= totalLines) {
            bugStatusBar.setMaximum(bugs);
            gameBugStatus.setText(Integer.toString(bugs) + "/" + Integer.toString(bugs));
        } else {
            bugStatusBar.setMaximum(maxBugs);
            gameBugStatus.setText(Integer.toString(bugs) + "/" + Integer.toString(maxBugs));
        }
        if (Core.games.get(selected).getStatus() >= 50) {
            gameBonus.setEnabled(true);

        }
        if (Core.games.get(selected).getStatus() >= 70) {
            gameBonus.setEnabled(false);
        }

        bugStatusBar.setMinimum(0);
        bugStatusBar.setValue(bugs);
        usedtime = Core.games.get(selected).getUsedTime();
        gameTimeStatus.setText(Integer.toString(usedtime));
        workGame.setEnabled(buttonWork);
        debugGame.setEnabled(buttonDebug);
//        gameBonus.setEnabled(bonusReady);
        statusBar.setMaximum(100);
        statusBar.setMinimum(0);
        statusBar.setValue(Core.games.get(selected).getStatus());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        gameWorkerPanel = new javax.swing.JPanel();
        gameName = new javax.swing.JLabel();
        labelLineStatus = new javax.swing.JLabel();
        gameLineStatus = new javax.swing.JLabel();
        lineStatusBar = new javax.swing.JProgressBar();
        labelBugStatus = new javax.swing.JLabel();
        gameBugStatus = new javax.swing.JLabel();
        bugStatusBar = new javax.swing.JProgressBar();
        workGame = new javax.swing.JButton();
        debugGame = new javax.swing.JButton();
        closeView = new javax.swing.JButton();
        gameCost = new javax.swing.JLabel();
        labelTimeStatus = new javax.swing.JLabel();
        gameTimeStatus = new javax.swing.JLabel();
        gameBonus = new javax.swing.JButton();
        gameInfo = new javax.swing.JButton();
        labelStatus = new javax.swing.JLabel();
        statusBar = new javax.swing.JProgressBar();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(startupcompanymanager2012.StartupCompanyManager2012App.class).getContext().getResourceMap(StartupCompanyManager2012gameWorker.class);
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("The Game Worker.title")); // NOI18N
        setName("The Game Worker"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                closedFrame(evt);
            }
        });

        gameWorkerPanel.setBackground(resourceMap.getColor("gameWorkerPanel.background")); // NOI18N
        gameWorkerPanel.setName("gameWorkerPanel"); // NOI18N

        gameName.setFont(resourceMap.getFont("gameName.font")); // NOI18N
        gameName.setText(resourceMap.getString("gameName.text")); // NOI18N
        gameName.setName("gameName"); // NOI18N

        labelLineStatus.setText(resourceMap.getString("labelLineStatus.text")); // NOI18N
        labelLineStatus.setName("labelLineStatus"); // NOI18N

        gameLineStatus.setText(resourceMap.getString("gameLineStatus.text")); // NOI18N
        gameLineStatus.setName("gameLineStatus"); // NOI18N

        lineStatusBar.setName("lineStatusBar"); // NOI18N

        labelBugStatus.setText(resourceMap.getString("labelBugStatus.text")); // NOI18N
        labelBugStatus.setName("labelBugStatus"); // NOI18N

        gameBugStatus.setText(resourceMap.getString("gameBugStatus.text")); // NOI18N
        gameBugStatus.setName("gameBugStatus"); // NOI18N

        bugStatusBar.setName("bugStatusBar"); // NOI18N

        workGame.setText(resourceMap.getString("workGame.text")); // NOI18N
        workGame.setName("workGame"); // NOI18N
        workGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                workGame(evt);
            }
        });

        debugGame.setText(resourceMap.getString("debugGame.text")); // NOI18N
        debugGame.setName("debugGame"); // NOI18N
        debugGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                debugGame(evt);
            }
        });

        closeView.setText(resourceMap.getString("closeView.text")); // NOI18N
        closeView.setName("closeView"); // NOI18N
        closeView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeView(evt);
            }
        });

        gameCost.setText(resourceMap.getString("gameCost.text")); // NOI18N
        gameCost.setName("gameCost"); // NOI18N

        labelTimeStatus.setText(resourceMap.getString("labelTimeStatus.text")); // NOI18N
        labelTimeStatus.setName("labelTimeStatus"); // NOI18N

        gameTimeStatus.setText(resourceMap.getString("gameTimeStatus.text")); // NOI18N
        gameTimeStatus.setName("gameTimeStatus"); // NOI18N

        gameBonus.setText(resourceMap.getString("gameBonus.text")); // NOI18N
        gameBonus.setName("gameBonus"); // NOI18N
        gameBonus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                playBonus(evt);
            }
        });

        gameInfo.setText(resourceMap.getString("gameInfo.text")); // NOI18N
        gameInfo.setName("gameInfo"); // NOI18N
        gameInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showGameInfo(evt);
            }
        });

        labelStatus.setText(resourceMap.getString("labelStatus.text")); // NOI18N
        labelStatus.setName("labelStatus"); // NOI18N

        statusBar.setName("statusBar"); // NOI18N

        javax.swing.GroupLayout gameWorkerPanelLayout = new javax.swing.GroupLayout(gameWorkerPanel);
        gameWorkerPanel.setLayout(gameWorkerPanelLayout);
        gameWorkerPanelLayout.setHorizontalGroup(
            gameWorkerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gameWorkerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(gameWorkerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(gameWorkerPanelLayout.createSequentialGroup()
                        .addComponent(statusBar, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(gameWorkerPanelLayout.createSequentialGroup()
                        .addGroup(gameWorkerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(gameWorkerPanelLayout.createSequentialGroup()
                                .addComponent(labelLineStatus)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(gameLineStatus))
                            .addGroup(gameWorkerPanelLayout.createSequentialGroup()
                                .addComponent(labelBugStatus)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(gameBugStatus))
                            .addGroup(gameWorkerPanelLayout.createSequentialGroup()
                                .addComponent(workGame, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(14, 14, 14)
                                .addComponent(debugGame)
                                .addGap(18, 18, 18)
                                .addComponent(gameBonus)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(gameWorkerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(closeView, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                                    .addComponent(gameInfo, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)))
                            .addComponent(bugStatusBar, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
                            .addComponent(lineStatusBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(gameWorkerPanelLayout.createSequentialGroup()
                        .addComponent(gameName)
                        .addGap(44, 44, 44)
                        .addComponent(gameCost)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 75, Short.MAX_VALUE)
                        .addComponent(labelTimeStatus)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(gameTimeStatus)
                        .addGap(43, 43, 43))
                    .addGroup(gameWorkerPanelLayout.createSequentialGroup()
                        .addComponent(labelStatus)
                        .addContainerGap(319, Short.MAX_VALUE))))
        );
        gameWorkerPanelLayout.setVerticalGroup(
            gameWorkerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gameWorkerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(gameWorkerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(gameName)
                    .addComponent(labelTimeStatus)
                    .addComponent(gameTimeStatus)
                    .addComponent(gameCost))
                .addGap(18, 18, 18)
                .addGroup(gameWorkerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelLineStatus)
                    .addComponent(gameLineStatus))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lineStatusBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(gameWorkerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelBugStatus)
                    .addComponent(gameBugStatus))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bugStatusBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(labelStatus)
                .addGap(7, 7, 7)
                .addComponent(statusBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(gameWorkerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(gameBonus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(gameWorkerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(workGame, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                        .addComponent(debugGame, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
                    .addGroup(gameWorkerPanelLayout.createSequentialGroup()
                        .addComponent(gameInfo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(closeView)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(gameWorkerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(gameWorkerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void checkBonusAvailable() {

        if (bonusReady == true) {

            gameBonus.setEnabled(true);
        } else {
            gameBonus.setEnabled(false);

        }
    }
    private void workGame(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_workGame

        int days = Integer.parseInt(JOptionPane.showInputDialog("Enter the amount of days you want to work: ", 0));
        Core.games.get(selected).elaborate(days);
        Core.games.get(selected).checkGame();
        checkBonusAvailable();
        StartupCompanyManager2012View.livelDate.setText(Core.calendar.toString());
        updateStats();


    }//GEN-LAST:event_workGame

    private void debugGame(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_debugGame
        int days = Integer.parseInt(JOptionPane.showInputDialog("Enter the amount of days you want to spend on debugging: ", 0));
        Core.games.get(selected).removeBugs(days);
        Core.games.get(selected).checkGame();
        checkBonusAvailable();
        StartupCompanyManager2012View.livelDate.setText(Core.calendar.toString());
        if (Core.games.get(selected).getDev().getWorking() == true) {
            updateStats();
        }


    }//GEN-LAST:event_debugGame

    private void closeView(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeView
        Core.calendarTimer.start();
        dispose();
    }//GEN-LAST:event_closeView

    private void closedFrame(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closedFrame
        Core.calendarTimer.start();
    }//GEN-LAST:event_closedFrame

    private void showGameInfo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showGameInfo
        JFrame mainFrame = StartupCompanyManager2012App.getApplication().getMainFrame();
        gameInfoBox = new StartupCompanyManager2012gameInfo();
        gameInfoBox.setLocationRelativeTo(mainFrame);
        StartupCompanyManager2012gameInfo.setSelected(selected);
        StartupCompanyManager2012App.getApplication().show(gameInfoBox);    }//GEN-LAST:event_showGameInfo

    private void playBonus(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playBonus
        gameBonus.setEnabled(false);

        JFrame mainFrame = StartupCompanyManager2012App.getApplication().getMainFrame();
        bonusBox = new SlidePuzzle();
        bonusBox.setLocationRelativeTo(mainFrame);
        StartupCompanyManager2012App.getApplication().show(bonusBox);


    }//GEN-LAST:event_playBonus

    /**
     * @param args the command line arguments
     */
    public static void init(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StartupCompanyManager2012gameWorker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StartupCompanyManager2012gameWorker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StartupCompanyManager2012gameWorker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StartupCompanyManager2012gameWorker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new StartupCompanyManager2012gameWorker().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JProgressBar bugStatusBar;
    private javax.swing.JButton closeView;
    public static javax.swing.JButton debugGame;
    private static javax.swing.JButton gameBonus;
    public static javax.swing.JLabel gameBugStatus;
    public static javax.swing.JLabel gameCost;
    private javax.swing.JButton gameInfo;
    public static javax.swing.JLabel gameLineStatus;
    private static javax.swing.JLabel gameName;
    public static javax.swing.JLabel gameTimeStatus;
    private javax.swing.JPanel gameWorkerPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel labelBugStatus;
    private javax.swing.JLabel labelLineStatus;
    private javax.swing.JLabel labelStatus;
    private javax.swing.JLabel labelTimeStatus;
    public static javax.swing.JProgressBar lineStatusBar;
    private static javax.swing.JProgressBar statusBar;
    public static javax.swing.JButton workGame;
    // End of variables declaration//GEN-END:variables
    private JFrame gameInfoBox = new JFrame();
}
